<template>
  <div class="app-container">
    <el-form @submit.prevent :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="体测项目名称" prop="xmMc">
        <el-input
          v-model="queryParams.xmMc"
          placeholder="请输入体测项目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
	  
	  
	  
    </el-form>
<!--  
v-hasPermi="['tc:project:add']"
v-hasRole="['admin', 'common']"
 
 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasRole="['admin', 'common']"
        >新增项目</el-button>
      </el-col>
	  
<!-- 	  
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tc:project:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tc:project:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['tc:project:export']"
        >导出</el-button>
      </el-col>
	  -->
	  
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
	  <el-table-column type="index" label="序号" width="55" />
      <el-table-column label="体测项目名称" align="center" prop="xmMc" />
	  <!-- <el-table-column label="编号" align="center" prop="id"/> -->
	  <el-table-column label="状态" align="center" prop="xmStatus">
		 <template #default="scope">
			<dict-tag :options="tc_qyzt" :value="scope.row.xmStatus"/>
		 </template>
      </el-table-column>
      <!-- <el-table-column label="设备" align="center" prop="shebei" /> -->
      <el-table-column label="图标" align="center" prop="icon" >
		 <template #default="scope">
			<svg-icon :icon-class="scope.row.icon" />
		 </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="200" v-hasRole="['admin', 'common']">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['tc:project:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tc:project:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改体测项目对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="projectRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="xmMc">
          <el-input v-model="form.xmMc" placeholder="请输入名称" />
        </el-form-item>
		
		<el-form-item label="启用状态" prop="xmStatus">
		  <el-select v-model="form.xmStatus" placeholder="请选择启用状态">
		    <el-option
		      v-for="dict in tc_qyzt"
		      :key="dict.value"
		      :label="dict.label"
		      :value="dict.value"
		    ></el-option>
		  </el-select>
		</el-form-item>
		<!-- <el-form-item label="图标" prop="icon"> -->
<!-- 		  <el-select v-model="form.icon" placeholder="请选择图标" >
		    <el-option
		      v-for="d in dept_self"
		      :key="d.value"
		      :label="d.label"
		      :value="d.value"
		    />
		  </el-select> -->
		  
		  <el-form-item label="图标" prop="icon">
		     <el-popover
		        placement="bottom-start"
		        :width="540"
		        trigger="click"
		     >
		        <template #reference>
		           <el-input v-model="form.icon" placeholder="点击选择图标" @blur="showSelectIcon" readonly>
		              <template #prefix>
		                 <svg-icon
		                    v-if="form.icon"
		                    :icon-class="form.icon"
		                    class="el-input__icon"
		                    style="height: 32px;width: 16px;"
		                 />
		                 <el-icon v-else style="height: 32px;width: 16px;"><search /></el-icon>
		              </template>
		           </el-input>
		        </template>
		        <icon-select ref="iconSelectRef" @selected="selected" :active-icon="form.icon" />
		     </el-popover>
		  </el-form-item>
		  
		<!-- </el-form-item>	 -->

		
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Project">
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/tc/project";
import SvgIcon from "@/components/SvgIcon";
import IconSelect from "@/components/IconSelect";
import { listShebei } from "@/api/tc/shebei";
import useUserStore from '@/store/modules/user'
var userStore=useUserStore();


const { proxy } = getCurrentInstance();
const { tc_qyzt } = proxy.useDict('tc_qyzt');

const projectList = ref([]);
const tcProjectTpList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedTcProjectTp = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const iconSelectRef = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    xmType: null,
    xmMc: null,
    xmStatus: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);
 onMounted(() => {
      // console.log('组件已经挂载')
	getList();  
	getShebeiList();
	
 })
/** 查询设备列表 */
function getShebeiList() {
  loading.value = true;
  let shebeiParams = {
    pageNum: 1,
    pageSize: 500,
    sbClientid: null,
    sbLoginuser: null,
    sbTctype: null,
    sbIszx: null,
    sbIsqy: null,
    sbXytype: null,
    sbTcdatatype: null,
    sbIsdel: null,
    sbJiedian: null,
    sbCreatetime: null,
    sbXxid: null,
    sbName: null,
	icon:null
  };
  listShebei(shebeiParams).then(response => {
    // shebeiList.value = response.rows;
	// console.log(response.rows);
    loading.value = false;
  });
}

/** 查询体测项目列表 */
function getList() {
  loading.value = true;
  listProject(queryParams.value).then(response => {
    projectList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    xmType: null,
    xmMc: null,
    xmNj: null,
    xmZb: null,
    xmDel: null,
    xmStatus: null,
    xmXxid: null,
    xmCreate: null,
    xmCreateby: null,
    xmUpdate: null,
    xmUpdateby: null,
	icon:null
  };
  tcProjectTpList.value = [];
  proxy.resetForm("projectRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加体测项目";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getProject(_id).then(response => {
	  console.log(response.data)
    form.value = response.data;
    tcProjectTpList.value = response.data.tcProjectTpList;
    open.value = true;
    title.value = "修改体测项目";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["projectRef"].validate(valid => {
    if (valid) {
      form.value.tcProjectTpList = tcProjectTpList.value;
      if (form.value.id != null) {
        updateProject(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProject(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除体测项目编号为"' + _ids + '"的数据项？').then(function() {
    return delProject(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 评分分类项目序号 */
function rowTcProjectTpIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 评分分类项目添加按钮操作 */
function handleAddTcProjectTp() {
  let obj = {};
  tcProjectTpList.value.push(obj);
}

/** 评分分类项目删除按钮操作 */
function handleDeleteTcProjectTp() {
  if (checkedTcProjectTp.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的评分分类项目数据");
  } else {
    const tcProjectTps = tcProjectTpList.value;
    const checkedTcProjectTps = checkedTcProjectTp.value;
    tcProjectTpList.value = tcProjectTps.filter(function(item) {
      return checkedTcProjectTps.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleTcProjectTpSelectionChange(selection) {
  checkedTcProjectTp.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('tc/project/export', {
    ...queryParams.value
  }, `project_${new Date().getTime()}.xlsx`)
}
/** 展示下拉图标 */
function showSelectIcon() {
  iconSelectRef.value.reset();
}
/** 选择图标 */
function selected(name) {
  form.value.icon = name;
}
</script>
